package com.jpragma.flexutils.renderers {
	import flash.events.Event;
	
	import mx.controls.DataGrid;
	import mx.controls.dataGridClasses.DataGridItemRenderer;
	import mx.controls.dataGridClasses.DataGridListData;
	
	public class BlinkWhenChangedRenderer extends DataGridItemRenderer {
		private var lastText:String = null;
		private var lastUID:String = null;
		private var frameCount:int = -1;
	
	
		override public function validateNow():void  {
			var needBlink:Boolean = false;
			var valueIncreased:Boolean = false;
			
	
			super.validateNow();
	
			if (!listData) {
				background = false;
				return;
			}
	
			var dgListData:DataGridListData = listData as DataGridListData;
			var dataGrid:DataGrid = dgListData.owner as DataGrid;
	
			if (dgListData.uid == lastUID) {
				if (lastText != dgListData.label) {
					needBlink = true;
					var oldNumValue:Number = new Number(lastText);
					var newNumValue:Number = new Number(dgListData.label);
					if (oldNumValue < newNumValue) {
						valueIncreased = true;
					}
				}
			}
			lastUID = dgListData.uid;
			lastText = dgListData.label;
	
			if (needBlink) {
				background = true;
				backgroundColor = (valueIncreased) ? 0x00FF00 : 0xFF0000;
				frameCount = 15;
				addEventListener("enterFrame", enterFrameHandler);
			}
		}
	
		private function enterFrameHandler(event:Event):void {
			frameCount--;
			if (frameCount < 0) {
				background = false;
				removeEventListener("enterFrame", enterFrameHandler);
			} else {
				if (backgroundColor >= 0xFF0000) {
					// red
					backgroundColor = (15 - frameCount) * 0x001111 + 0xFF0000;
				} else {
					// green 
					backgroundColor = (15 - frameCount) * 0x110011 + 0x00FF00;
				}
			}
		}		
	}
}